package com.ld.admin.service.oa;

import java.util.Date;

import org.springframework.stereotype.Service;

import com.ld.admin.model.oa.OaBooksBorrowRecordModel;
import com.ld.admin.model.oa.OaBooksManageModel;
import com.ld.common.config.OAStaticParams;
import com.ld.common.service.ProjectBasicService;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.PageNavigationBean;
import com.ld.shieldsb.dao.model.QueryModel;

/**
 * 
 * 书籍管理服务类，@Service注解用于自动注入
 * 
 * @ClassName OaBooksManageService
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年06月12日 14:29:47
 *
 */
@Service
public class OaBooksManageService extends ProjectBasicService {
    /**
     * 增加借阅记录
     * 
     * @Title addOrUpdateBorrowRecord
     * @author 吕凯
     * @date 2019年6月12日 下午3:24:35
     * @param borrowRecordModel
     * @param update
     * @return Result
     */
    public Result addOrUpdateBorrowRecord(OaBooksBorrowRecordModel borrowRecordModel, boolean update) {
        // 多个参数的写法
//        List<Property<OaBooksBorrowRecordModel, ?>> propertys = new ArrayList<>();
//        propertys.add(OaBooksBorrowRecordModel::getReturnTime);
//        Result result = super.addOrUpdate(borrowRecordModel, update, propertys); // returnTime即使为空也更新
        Result result = super.addOrUpdate(borrowRecordModel, update, OaBooksBorrowRecordModel::getReturnTime); // returnTime即使为空也更新
        if (result.getSuccess()) { // 成功，则将最新的借阅记录更新至书籍中
            OaBooksManageModel bookModel = new OaBooksManageModel();
            if (update) {
                borrowRecordModel = getOne(OaBooksBorrowRecordModel.class, borrowRecordModel.getId());
            }
            bookModel.setId(borrowRecordModel.getBookid());
            QueryModel borrowQuery = getNormalQueryModel();
            borrowQuery.addEq("bookid", bookModel.getId());
            borrowQuery.setOrder("borrowTime", true);
            OaBooksBorrowRecordModel borrowRecord = getOne(OaBooksBorrowRecordModel.class, borrowQuery); // 获取本书的最新的一条借阅记录
            if (borrowRecord != null) {
                if (borrowRecord.getReturnTime() != null && borrowRecord.getReturnTime().before(new Date())) { // 已归还，则可借阅
                    bookModel.setBorrowState(OAStaticParams.BOOK_CANBORROW_YES);
                } else { // 未填写归还日期或者归还日志晚于今天，则不可借阅
                    bookModel.setBorrowState(OAStaticParams.BOOK_CANBORROW_NO);
                    bookModel.setBorrowerId(borrowRecord.getBorrowerId());
                    bookModel.setBorrowerName(borrowRecord.getBorrowerName());
                }
                getBaseDAO().update(bookModel);
            }
        }
        return result;
    }

    /**
     * 获取我的借阅信息
     * 
     * @Title getMyBorrowPageBean
     * @author 吕凯
     * @date 2019年6月13日 下午5:06:08
     * @return PageNavigationBean<OaBooksBorrowRecordModel>
     */
    public PageNavigationBean<OaBooksBorrowRecordModel> getMyBorrowPageBean() {
//        QueryModel queryModel = super.getSearchQueryModel(request);
//        queryModel.addEq(OaBooksBorrowRecordModel::getBorrowerId, Web.Util.getSessionUserId(request));
//        PageNavigationBean<OaBooksBorrowRecordModel> pageBean = super.getPageBeanAndLinks(request, OaBooksBorrowRecordModel.class,
//                queryModel);

        QueryModel queryModel = super.getSearchQueryModel(request, OaBooksBorrowRecordModel.class); // 主表查询
//        Web.Parameter.dealReqParams(request, queryModel);
        queryModel.addEq(OaBooksBorrowRecordModel::getBorrowerId, Web.Util.getSessionUserId(request));
        queryModel.setOrder(OaBooksBorrowRecordModel::getBorrowTime, true);
        queryModel.setQueryLink(true);
        PageNavigationBean<OaBooksBorrowRecordModel> pageBean = getPageBeanByJoin(request, OaBooksBorrowRecordModel.class,
                OaBooksBorrowRecordModel::getBook, queryModel);
        // 查询书的信息,已改为自动查询
//        if (pageBean != null && ListUtils.isNotEmpty(pageBean.getResultList())) {
//            for (OaBooksBorrowRecordModel model : pageBean.getResultList()) {
//                model.setBook(getOne(OaBooksManageModel.class, model.getBookid()));
//            }
//        }
        return pageBean;
    }

}
